package sipoc.messaging.component;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Component;

import sipoc.license.LicenseManager;
import sipoc.model.HbResponse;

@Component
public class LicenseOutboundAdapter {
	@Autowired
	LicenseManager licenseManager;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(LicenseOutboundAdapter.class);

	@ServiceActivator(inputChannel = "licenseManagerChannel")
	public HbResponse get(@Header String countryCode, @Header String assetId) {
		LOGGER.info("License received for assetId {} and country {}", assetId,
				countryCode);

		// enrich response with some other content
		String license = getLicenseFromSimulatedCallTo3rdParty();
		licenseManager.store(countryCode, assetId, license);

		HbResponse hbResponse = new HbResponse("LICENSE OK");
		hbResponse.setLicense(license);
		return hbResponse;
	}

	private String getLicenseFromSimulatedCallTo3rdParty() {
		return UUID.randomUUID().toString();
	}

}
